package com.aihna.system.controller.system;

import java.util.List;
import java.util.stream.Collectors;

import com.aihna.common.pay.domain.bo.SysPaymentAppBo;
import com.aihna.common.pay.domain.bo.SysPaymentChannelBo;
import com.aihna.common.pay.domain.vo.SysPaymentAppVo;
import com.aihna.common.pay.domain.vo.SysPaymentChannelVo;
import com.aihna.common.pay.service.ISysPaymentAppService;
import com.aihna.common.pay.service.ISysPaymentChannelService;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.aihna.common.idempotent.annotation.RepeatSubmit;
import com.aihna.common.log.annotation.Log;
import com.aihna.common.web.core.BaseController;
import com.aihna.common.mybatis.core.page.PageQuery;
import com.aihna.common.core.domain.R;
import com.aihna.common.core.validate.AddGroup;
import com.aihna.common.core.validate.EditGroup;
import com.aihna.common.log.enums.BusinessType;
import com.aihna.common.excel.utils.ExcelUtil;
import com.aihna.common.mybatis.core.page.TableDataInfo;

/**
 * 支付应用
 *
 * @author Joseph.Chen
 * @date 2024-03-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/paymentApp")
public class SysPaymentAppController extends BaseController {

    private final ISysPaymentAppService sysPaymentAppService;

    private final ISysPaymentChannelService sysPaymentChannelService;

    /**
     * 查询支付应用列表
     */
    @SaCheckPermission("system:paymentApp:list")
    @GetMapping("/list")
    public TableDataInfo<SysPaymentAppVo> list(SysPaymentAppBo bo, PageQuery pageQuery) {
        TableDataInfo<SysPaymentAppVo> tableDataInfo = sysPaymentAppService.queryPageList(bo, pageQuery);
        List<SysPaymentAppVo> sysPaymentAppVos = tableDataInfo.getRows();
        for (SysPaymentAppVo sysPaymentAppVo : sysPaymentAppVos) {
            SysPaymentChannelBo sysPaymentChannelBo = new SysPaymentChannelBo();
            sysPaymentChannelBo.setAppId(sysPaymentAppVo.getId());
            List<SysPaymentChannelVo> sysPaymentChannelVos = sysPaymentChannelService.queryList(sysPaymentChannelBo);
            List<String> channelCodes = sysPaymentChannelVos.stream().map(SysPaymentChannelVo -> SysPaymentChannelVo.getCode()).collect(Collectors.toList());
            sysPaymentAppVo.setChannelCodes(channelCodes);
        }

        tableDataInfo.setRows(sysPaymentAppVos);
        return tableDataInfo;
    }

    /**
     * 导出支付应用列表
     */
    @SaCheckPermission("system:paymentApp:export")
    @Log(title = "支付应用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysPaymentAppBo bo, HttpServletResponse response) {
        List<SysPaymentAppVo> list = sysPaymentAppService.queryList(bo);
        ExcelUtil.exportExcel(list, "支付应用", SysPaymentAppVo.class, response);
    }

    /**
     * 获取支付应用详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:paymentApp:query")
    @GetMapping("/{id}")
    public R<SysPaymentAppVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysPaymentAppService.queryById(id));
    }

    /**
     * 新增支付应用
     */
    @SaCheckPermission("system:paymentApp:add")
    @Log(title = "支付应用", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysPaymentAppBo bo) {
        return toAjax(sysPaymentAppService.insertByBo(bo));
    }

    /**
     * 修改支付应用
     */
    @SaCheckPermission("system:paymentApp:edit")
    @Log(title = "支付应用", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysPaymentAppBo bo) {
        return toAjax(sysPaymentAppService.updateByBo(bo));
    }

    /**
     * 删除支付应用
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:paymentApp:remove")
    @Log(title = "支付应用", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysPaymentAppService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 支付应用状态修改
     */
    @SaCheckPermission("system:paymentApp:edit")
    @Log(title = "支付应用状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeDefaultStatus(@RequestBody SysPaymentAppBo bo) {
        return toAjax(sysPaymentAppService.updateByBo(bo));
    }
}
